const path = require('path')

const Koa = require('koa');
const { koaBody } = require('koa-body');
const koaStatic = require('koa-static');
const parameter = require('koa-parameter')

const errHandler = require('./errHandler');
const router = require('../router')
const app = new Koa();

app.use(koaBody({
    multipart: true,
    formidable: {
        // 在配置选项中不推荐使用相对路径./src/upload
        uploadDir: path.join(__dirname, '../uploads'),
        keepExtensions: true,
    },
    parsedMethods: ['POST', 'PUT', 'PATCH', 'DELETE']
}));
app.use(koaStatic(path.join(__dirname, '../upload')))
app.use(parameter(app))
app.use(router.routes()).use(router.allowedMethods())

// 进行统一的错误处理
app.on('error', errHandler);

module.exports = app;